import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)
const getShopingCarList = () => {
	let shoping_car_list = uni.getStorageSync('shoping_car_list');
	if (shoping_car_list) {
		uni.setStorageSync('shoping_car_list', shoping_car_list);
		return shoping_car_list;
	} else {
		uni.setStorageSync('shoping_car_list', []);
		return []
	}

}
export default new Vuex.Store({
	state: {
		shopingCarList: getShopingCarList()
	},
	getters: {
		totalGoodsNum(state) {
			let totalNum = state.shopingCarList.reduce((prev, item) => {
				prev += item.num;
				return prev;
			}, 0);
			if (totalNum) {
				return totalNum;
			} else {
				return 0;
			}
		},
		cur_address_point_id(state) {
			let cur_address_point = uni.getStorageSync('cur_address_point');
			return cur_address_point.id;
		},
		cur_address_point(state) {
			let cur_address_point = uni.getStorageSync('cur_address_point');
			return cur_address_point;
		},
		totalPrice(state) {
			let totalPrice = state.shopingCarList.reduce((prev, item) => {
				prev += item.num * item.price;
				return prev;
			}, 0);
			if (totalPrice) {
				return totalPrice.toFixed(2);
			} else {
				return 0;
			}
		}
	},
	mutations: {
		SET_SHOPING_CAR_LIST(state, shopingCarList) {
			state.shopingCarList = shopingCarList;
			uni.setStorageSync('shoping_car_list', shopingCarList);
		},
	},
	actions: {

	}
})
